package thread.novisibility;

/**
 * Created by IntelliJ IDEA.
 * User: liuliwei
 * Date: 2017-09-28
 * Time: 下午4:54
 * Description: ${description}
 */
public class T1 {
    public static void main(String[] args) {
        TestInteger testInteger = new TestInteger();
        new Thread(new readRunnable(testInteger)).start();
        new Thread(new WriteRunnable(testInteger,0)).start();
    }
}

class readRunnable implements Runnable {
    private TestInteger testInteger;

    public readRunnable(TestInteger testInteger) {
        this.testInteger = testInteger;
    }

    @Override
    public void run() {
        while (true) {
            int value = testInteger.getValue();
            if (value==1000) {
                System.out.println(value);
            }
        }
    }
}


class WriteRunnable implements Runnable {
    private TestInteger testInteger;
    private int value;

    public WriteRunnable(TestInteger testInteger,int value) {
        this.testInteger = testInteger;
        this.value = value;
    }

    @Override
    public void run() {
        while (true) {
            testInteger.setValue(value++);
        }
    }
}

